﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace edu.wpi.cs.Project1
{
    /// <summary>
    /// General utitlity functions for the project.
    /// </summary>
    public static class Utility
    {
        /// <summary>
        /// Translates the object to the globally defined Z position.
        /// </summary>
        /// <param name="obj">The game object to translate.</param>
        public static void TranslateToXYLayer(GameObject obj, float z)
        {
            if (obj == null)
                return;

            Vector3 pos = obj.transform.position;
            float ratio = z / pos.z;
            pos.x *= ratio;
            pos.y *= ratio;
            pos.z = z;

            obj.transform.position = pos;
        }

        /// <summary>
        /// Rotates the game object orthogonal to the X/Y layer, to that only
        /// Z-rotation is allowed.
        /// </summary>
        /// <param name="obj">The game object to rotate.</param>
        public static Quaternion RotateOrthogonalToXYLayer(GameObject obj)
        {
            if (obj == null)
                return Quaternion.identity;

            return RotateOrthogonalToXYLayer(obj.transform.rotation);
        }

        /// <summary>
        /// Rotates the game object orthogonal to the X/Y layer, to that only
        /// Z-rotation is allowed.
        /// </summary>
        /// <param name="q">The quaternion to rotate.</param>
        public static Quaternion RotateOrthogonalToXYLayer(Quaternion q)
        {
            Quaternion eq = Quaternion.Euler(0, 0, q.eulerAngles.z);
            return eq;
        }

        /// <summary>
        /// Crates a new ball.
        /// </summary>
        /// <param name="name">The name of the ball</param>
        /// <param name="position">The position of the ball</param>
        /// <param name="rotation">The rotation of the ball.</param>
        /// <returns>The ball object</returns>
        public static GameObject CreateBall(string name, Vector3 position, Quaternion rotation)
        {
            GameObject ballPrefab = Resources.Load("BallPrefab") as GameObject;
            GameObject ball = UnityEngine.Object.Instantiate(ballPrefab, position, rotation) as GameObject;
            ball.name = name;
            return ball;
        }

        /// <summary>
        /// Creates a new popup text.
        /// </summary>
        /// <param name="position">The position of the text.</param>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static GameObject CreatePopupText(Vector3 position, string text)
        {
            GameObject popupPrefab = Resources.Load("PopupTextPrefab") as GameObject;
            GameObject popup = UnityEngine.Object.Instantiate(popupPrefab, position, Quaternion.identity) as GameObject;
            popup.GetComponent<TextMesh>().text = text;
            popup.name = "popup";
            return popup;
        }
    }
}
